package com.wbp.cms.web.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wbp.cms.client.customer.domain.CustomerDO;
import com.wbp.cms.client.result.BaseResultCode;
import com.wbp.cms.client.result.Result;
import com.wbp.cms.client.user.domain.UserDO;
import com.wbp.cms.client.user.service.UserService;
import com.wbp.cms.client.util.JsonUtils;
import com.wbp.cms.client.util.MD5Utils;
import com.wbp.cms.web.util.SessionUtil;

@Controller("loginMgrController")
@RequestMapping("/console")
public class LoginMgrController {

  @Resource(name = "userService")
  private UserService userService;

  @RequestMapping("/login")
  public ModelAndView toLogin() {

	ClassLoader classloader = this.getClass().getClassLoader() ;
	ClassLoader cls = classloader.getParent() ;
	
	System.out.println("LoginMgrController parent cls:"+cls);
	System.out.println("LoginMgrController parent parent cls:"+cls.getParent());
	System.out.println("LoginMgrController parent parent parent cls:"+cls.getParent().getParent());
	System.out.println("LoginMgrController parent parent parent parent cls:"+cls.getParent().getParent().getParent());
	System.out.println("LoginMgrController thread classloader:"+this.getClass().getClassLoader());
	System.out.println("LoginMgrController thread context classloader:"+Thread.currentThread().getContextClassLoader());
	System.out.println("LoginMgrController thread  classloader:"+Thread.currentThread().getClass().getClassLoader());
	   
	CustomerDO c  = new CustomerDO();
	c.setAddress("dfsfdfdffffffffffffff");
	System.out.println("LoginMgrController getaddress:"+c.getAddress());
	System.out.println("c classloader:"+c.getClass().getClassLoader());
	
	    
    ModelAndView mv = new ModelAndView();
    mv.setViewName("admin/login");
    return mv;
  }
  
  @RequestMapping("/loginout")
  public ModelAndView  toLoginOut(HttpServletRequest request){
    if(SessionUtil.getCurrentUser(request.getSession()) !=null )
      SessionUtil.removeCurrentUser(request.getSession());
    
    ModelAndView mv = new ModelAndView();
    mv.setViewName("redirect:/console/login");
    return mv;
  }

  /**
   * 登录页面
   * 
   * @return
   */
  /*@RequestMapping(value = "/dologin", method = {RequestMethod.GET, RequestMethod.POST})
  public ModelAndView doLogin(HttpServletRequest request, @RequestParam(value = "userName",
      required = true, defaultValue = "") String userName, @RequestParam(value = "passWord",
      required = true, defaultValue = "") String passWord ) throws Exception {

    ModelAndView mv = new ModelAndView();

    mv.setViewName("forward:/console/login"); // 失败回原页面url 

    BaseResultCode validResultCode = validLogin(userName,passWord)  ;
    if(validResultCode.getErrCode() !=BaseResultCode.SUCCESS.getErrCode() ) {
       mv.addObject("errorCode", validResultCode.getErrCode());
       mv.addObject("errorMsg", validResultCode.getErrMsg() );
       return mv ;
    }
    passWord = MD5Utils.strToMD5(passWord + userName);

    Result<UserDO> userResult = userService.login(userName, passWord);
    if (userResult.isFailed()) {
      mv.addObject("errorCode", userResult.getErrCode());
      mv.addObject("errorMsg", userResult.getErrMsg() );
      return mv ;
    }

    SessionUtil.addUserToSession(request.getSession(), userResult.getData()); 
    mv.setViewName("redirect:/console/main"); // 成功　主页
    return mv;
  }*/
  
  
  @RequestMapping(value = "/dologin", method = RequestMethod.POST ,produces = "application/json;charset=UTF-8")
  @ResponseBody
  public String doLogin(HttpServletRequest request, @RequestParam(value = "userName",
      required = true, defaultValue = "") String userName, @RequestParam(value = "passWord",
      required = true, defaultValue = "") String passWord ) throws Exception {

    BaseResultCode validResultCode = validLogin(userName,passWord)  ;
    if(validResultCode.getErrCode() !=BaseResultCode.SUCCESS.getErrCode() ) {
      return  JsonUtils.toJson(Result.buildError(validResultCode)) ;
    }
    passWord = MD5Utils.strToMD5(passWord + userName);

    Result<UserDO> userResult = userService.login(userName, passWord);
    if (userResult.isFailed()) {
      return  JsonUtils.toJson(Result.buildError(userResult.getErrMsg())) ;
    }

    SessionUtil.addUserToSession(request.getSession(), userResult.getData()); 
     
    return JsonUtils.toJson(Result.buildSuccess(userResult.getData().getId()) ) ;
  }
  
  
  
  /**
   * 主页
   * @param request
   * @param userName
   * @param passWord
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/main", method = {RequestMethod.GET})
  public ModelAndView toIndex(HttpServletRequest request ) throws Exception {

    ModelAndView mv = new ModelAndView();
 
    mv.setViewName("admin/index"); // 成功　主页
    return mv;
  }
  
  
  
  
  
   private  BaseResultCode  validLogin(String userName,String passWord){
      if(StringUtils.isEmpty(userName))
        return BaseResultCode.USER_USERNAME_NOT_EMPTY ;
      if(StringUtils.isEmpty(passWord))
        return BaseResultCode.USER_PASSWORD_NOT_EMPTY ;
      
      /*if( !CommonUtil.isMobile(userName))
        return  BaseResultCode.USER_MOBILE_NUMBER_IS_INVALID ;
      */
      if( !(passWord.length() >=6 && passWord.length() <= 8) )
        return   BaseResultCode.USER_PASSWORD_LENGTH_IN6AND8 ;
            
      return BaseResultCode.SUCCESS ;
   }
}
